package com.itlaobing.service;

import com.itlaobing.mapper.SystemUserMapper;
import com.itlaobing.pojo.SystemUser;
import com.itlaobing.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemUserService {
    @Autowired
    SystemUserMapper systemUserMapper;

    /**
     * 登陆查询
     * @param user
     * @return
     */
    public SystemUser findBySystemUser(SystemUser user) {
        SystemUser systemUser=systemUserMapper.findBySystemUser(user);
        return  systemUser;
    }

    /**
     * ajax通过账号查询用户
     * @param account
     * @return
     */
    public Boolean findByAccount(String account) {
        SystemUser systemUser=systemUserMapper.findByAccount(account);
        if(systemUser==null){
            return  false;
        }
        return true;
    }

    /**
     * 通过id和状态来查询
     * @param systemUser
     * @return
     */
    public SystemUser findByIdAndStatus(SystemUser systemUser) {
        return  systemUserMapper.findByIdAndStatus(systemUser);
    }

    /**
     * 根据id来修改登陆时间
     * @param map
     * @return
     */
    @Transactional
    public int updateLoginTimeById(HashMap<String,Object> map) {
        return  systemUserMapper.updateLoginTimeById(map);
    }

    /**
     * 通过用户id来修改密码
     * @param map
     * @return
     */
    @Transactional
    public Boolean updatePassById(HashMap<String,Object> map) {
        int i=systemUserMapper.updatePassById(map);
        if(i>0){
            return  true;
        }
        return false;
    }

    /**
     * 保存图片地址到数据库
     * @param map
     * @return
     */
    @Transactional
    public boolean UpdateHeadImageById(Map<String,Object> map) {

        int i = systemUserMapper.UpdateHeadImageById(map);

        if(i>0){
            return true;
        }
        return false;
    }

    /**
     * 查询总条数
     * @param page
     * @return
     */
    public int findCountByPage(PageUtil<SystemUser> page) {
        return systemUserMapper.findCountByPage(page);
    }

    /**
     * 获取分页数据
     * @param page
     * @return
     */
    public List<SystemUser> findUserByPage(PageUtil<SystemUser> page) {

        return systemUserMapper.findUserByPage(page);

    }

    /**
     * 创建管理员账号
     * @param user
     * @return
     */
    @Transactional
    public boolean saveUser(SystemUser user) {
        int i=systemUserMapper.saveUser(user);
        if(i>0){
            return true;
        }
        return false;
    }

    /**
     * 通过id来查询用户信息
     * @param id
     * @return
     */
    public SystemUser findOneById(String id) {
        return  systemUserMapper.selectByPrimaryKey(id);
    }

    /**
     * 锁定、解锁用户
     * @param map
     * @return
     */
    @Transactional
    public boolean updateStausById(Map<String,Object> map) {
        int i=systemUserMapper.updateStatusById(map);
        if(i>0){
            return true;
        }
        return false;
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    public Boolean deleteById(String id) {
        int i=systemUserMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }
}
